/*
 * Copyright(c) 2020-2100 万达信息股份有限公司 版权所有
 * WONDERS INFORMATION CO., LTD. ALL RIGHTS RESERVED.
 */

package com.wondersgroup.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.wondersgroup.entity.Product;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: huym
 * @version: 1.0
 * @date: 2020/11/18
 */
@RestController
public class ProductController {

    @Value("${server.port}")
    private int port;

    @GetMapping("/product/showMsg")
    public String showMsg(){
        return "商品服务，端口："+port;
    }

    @GetMapping("/product/findOne")
    @HystrixCommand(defaultFallback = "breakFall")
    public Map<String,Object> findOne(@RequestParam(name = "productId") String productId){
        if(Long.parseLong(productId)<0){
            throw new RuntimeException("productId不能小于0");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("msg","GET查询商品信息调用成功，当前服务端口："+port);
        map.put("status", true);
        map.put("productId", productId);
        return map;
    }

    @PostMapping("/product/save")
    public Map<String, Object> save(@RequestParam("name") String name){
        Map<String,Object> map = new HashMap<>();
        map.put("msg","POST查询商品信息调用成功，当前服务端口："+port);
        map.put("status", true);
        map.put("name", name);
        return map;
    }

    @PostMapping("/product/saveProduct")
    public Map<String, Object> saveProduct(@RequestBody Product product){
        Map<String, Object> map = new HashMap<>();
        map.put("msg","保存商品信息调用成功，当前服务端口："+port);
        map.put("status",true);
        map.put("product",product);
        return map;
    }

    @RequestMapping("/product/complex")
    public  Map<String,Object> complex(@RequestBody Product product, @RequestParam String name){
        Map<String, Object> map = new HashMap<>();
        map.put("msg","保存商品信息调用成功，当前服务端口："+port);
        map.put("status",true);
        map.put("product",product);
        return map;
    }

    @GetMapping("/product/findAll")
    public Map<String, Object> findAll(){
        Map<String, Object> map = new HashMap<>();
        map.put("status", "ok");
        map.put("msg","查询所有商品，端口："+port);
        return map;
    }

    @GetMapping("/product/break")
    @HystrixCommand(fallbackMethod = "myBreakFall")
    //@HystrixCommand(defaultFallback = "breakFall")
    public String testBreak(Integer id){
        if (id<0){
            throw new RuntimeException("参数不可小于0");
        }
        return "商品服务调用成功，id="+id;
    }

    /**
     * fallback方法参数必须和testBreak方法参数一致
     * @param id
     * @return
     */
    public String myBreakFall(Integer id){
        return "服务熔断，当前数据不合法："+id;
    }

    /**
     * 通用fallback方法，不需要参数
     * @return
     */
    public Map<String,Object> breakFall(){
        Map<String,Object> map = new HashMap<>();
        map.put("msg", "服务熔断，服务不可用");
        map.put("status", false);
        return map;
    }

}
